package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Associado;

public class AssociadoDAO {

	private Connection connection;

	public AssociadoDAO() throws SQLException {
		this.connection = ConnectionFactory.getConnection();
	}

	public AssociadoDAO(Long idAssociado) throws SQLException {
		this.connection = ConnectionFactory.getConnection();
		
	}
	
	public void adiciona(Associado associado) throws SQLException {

		PreparedStatement stmt = this.connection.
			prepareStatement("insert into associado (login, email, senha, cpf, telefone, endereco) values (?, ?, ?,?,?,?)");
		
		stmt.setString(1,associado.getLogin());
		stmt.setString(2,associado.getEmail());
		stmt.setString(3,associado.getSenha());
		stmt.setString(4,associado.getCpf());
		stmt.setString(5,associado.getTelefone());
		stmt.setString(6,associado.getEndereco());
		
		stmt.execute();
		stmt.close();
	}
	
	public List<Associado> getLista() throws SQLException {
		
		PreparedStatement stmt = 
			this.connection.prepareStatement("select * from associado order by nome;");
		ResultSet rs = stmt.executeQuery();
		
		List<Associado> associados = new ArrayList<Associado>();		
		while (rs.next()) {
			Associado associado = new Associado();
			carregaAssociado(rs, associado);
			associados.add(associado);
		}

		rs.close();
		stmt.close();
		return associados;
	}

	private void carregaAssociado(ResultSet rs, Associado associado)
			throws SQLException {
		associado.setCpf(rs.getString("cpf"));
		associado.setEndereco(rs.getString("endereco"));
		associado.setIdAssociado(Long.parseLong(rs.getString("idAssociado")));
		associado.setLogin(rs.getString("login"));
		associado.setSenha(rs.getString("senha"));
		associado.setTelefone(rs.getString("telefone"));
		associado.setNome(rs.getString("nome"));
		associado.setEmail(rs.getString("email"));
	}

	public void altera(Associado associado) throws SQLException {
		PreparedStatement stmt = 
			connection.prepareStatement("update associado set nome=?, email=?, endereco=?, telefone=?, login=?, senha=?, cpf=? where idAnunciado=?");
		
		stmt.setString(1, associado.getNome());
		stmt.setString(2, associado.getEmail());
		stmt.setString(3, associado.getEndereco());
		stmt.setString(4, associado.getTelefone());
		stmt.setString(5, associado.getLogin());
		stmt.setString(6, associado.getSenha());
		stmt.setString(7, associado.getCpf());
		stmt.setLong(8, associado.getIdAssociado());

		stmt.execute();
		stmt.close();
	}

	public void remove(Associado associado) throws SQLException {
		PreparedStatement stmt = 
			connection.prepareStatement("delete from associado where idAssociado=?");
		stmt.setLong(1, associado.getIdAssociado());
		stmt.execute();
		stmt.close();
	}

	public boolean existeEmail(String email) throws SQLException {
		PreparedStatement stmt = 
			this.connection.prepareStatement("select * from associado where email=?");
		stmt.setString(1, email );
		ResultSet rs = stmt.executeQuery();
		
		boolean ret = rs.next();
		
		rs.close();
		stmt.close();

		return ret; 

	}

	public boolean existeLogin(String login) throws SQLException {
		PreparedStatement stmt = 
			this.connection.prepareStatement("select * from associado where login=?");
		stmt.setString(1, login );
		ResultSet rs = stmt.executeQuery();
		
		boolean ret = rs.next();
		
		rs.close();
		stmt.close();

		return ret; 
	}

	public Associado getAssociado(String login, String senha) throws SQLException {
		PreparedStatement stmt = 
			this.connection.prepareStatement("select * from associado where login=? and senha=?");
		stmt.setString(1, login );
		stmt.setString(2, senha );
		ResultSet rs = stmt.executeQuery();
		
		boolean existeUm = rs.next();
		if( existeUm && rs.next() ) {
			existeUm = false;
		}
		
		Associado associado = null;
		if (existeUm) {
			rs.first();
			associado = new Associado();
			carregaAssociado(rs, associado);		
		}

		rs.close();
		stmt.close();
		return associado;

	}

	public Associado getAssociado(long idAssociado) throws SQLException {
		PreparedStatement stmt = 
			this.connection.prepareStatement("select * from associado where idassociado=?");
		stmt.setLong(1, idAssociado );
		
		ResultSet rs = stmt.executeQuery();
		
		boolean existeUm = rs.next();
		if( existeUm && rs.next() ) {
			existeUm = false;
		}
		
		Associado associado = null;
		if (existeUm) {
			rs.first();
			associado = new Associado();
			carregaAssociado(rs, associado);		
		}

		rs.close();
		stmt.close();
		return associado;

	}

}
